
import { Data, DateReg } from "../../types/index";

import {
  markRaw,
} from "vue";

import type {
  Ref, UnwrapNestedRefs
} from "vue";
/* 日期转化 */
export function formatDate(NewDtime: Date): string {
  let datetime = new Date(NewDtime);
  let year = datetime.getFullYear();
  let month: string = (datetime.getMonth() + 1).toString();//js从0开始取
  let date: string = datetime.getDate().toString();
  let hour: string = datetime.getHours().toString();
  let minutes: string = datetime.getMinutes().toString();
  let second: string = datetime.getSeconds().toString();

  if (parseInt(month) < 10) {
    month = "0" + month;
  }
  if (parseInt(date) < 10) {
    date = "0" + date;
  }
  if (parseInt(hour) < 10) {
    hour = "0" + hour;
  }
  if (parseInt(minutes) < 10) {
    minutes = "0" + minutes;
  }
  if (parseInt(second) < 10) {
    second = "0" + second;
  }
  let time = year + "-" + month + "-" + date + " " + hour + ":" + minutes + ":" + second; //2009-06-12 17:18:05
  return time;
}

export function deepCopyRef<T extends Ref | UnwrapNestedRefs<T | any>>(obj: T): T {
  let _obj: any = markRaw({}); //标记一个对象，使其永远不会转换为 proxy。返回对象本身。
  return Object.assign(_obj, obj);
}

// function getCookie(name: string) {
//   let reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
//   let arr: any = document.cookie.match(reg);
//   if (arr) return unescape(arr[2]);
//   else return null;
// }

// function DateStrKey(): string {
//   let date: Date = new Date();
//   let y: string = date.getFullYear().toString();
//   let m: string = (date.getMonth() + 1).toString();;
//   let d: string = date.getDate().toString();

//   if (parseInt(m) < 10) {
//     m = "0" + m;
//   }
//   if (parseInt(d) < 10) {
//     d = "0" + d;
//   }
//   return `${y}-${m}-${d}`
//   // console.log("现在是 " + y + "年" + m + "月" + d + "日 星期 " + day + " " + h + ":" + mi + ":" + s + ":" + ms);
// }







// 解密



/**
* @author 伟健 / 张
* @name 根据key赋值
* @param
* @return original
*/
const hasOwnProperty = Object.prototype.hasOwnProperty
export const setval = (original: Data, _from: Data): Data => {
  for (let key in original) {
    if (hasOwnProperty.call(_from, key)) {
      original[key] = _from[key];
    }
  }
  return original
}



export let dateFormat = (date: Date, fmt: string): string => {
  if (!date) return "";
  let o: DateReg = {
    "M+": date.getMonth() + 1, // 月份
    "d+": date.getDate(), // 日
    "h+": date.getHours(), // 小时
    "m+": date.getMinutes(), // 分
    "s+": date.getSeconds(), // 秒
    "q+": Math.floor((date.getMonth() + 3) / 3), // 季度
    S: date.getMilliseconds(), // 毫秒
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (let k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k as keyof DateReg] as any : ("00" + o[k as keyof DateReg] as any).substr(("" + o[k as keyof DateReg] as any).length));
    }
  }
  return fmt;
}
/**
* @author 伟健 / 张
* @name 合并对象
* @param 
* @return 
*/

export const extend = (to: Data, _from: Data): Data => {
  for (let key in _from) {
    if (to[key] == undefined || to[key] == null || to[key] === '') {
      to[key] = _from[key];
    }
  }
  return to
}

export const getPosition = (el: HTMLElement): { readonly width: number, readonly height: number, readonly x: number, readonly y: number } => {

  let { width, height, x, y } = el.getBoundingClientRect()
  return { width, height, x, y }

}
type ScrollDirection = "scrollTop" | "scrollLeft"
// 获取滚动条距离
export const getScroll = (direction: ScrollDirection, el?: HTMLElement) => {
  if (el) {
    return el[direction] || el[direction]
  }
  return document.body[direction] || document.documentElement[direction]
}


export const classNameToArray = (cls = '') =>
  cls.split(' ').filter((item) => !!item.trim())

export const addClass = (el: Element, cls: string) => {
  if (!el || !cls.trim()) return
  el.classList.add(...classNameToArray(cls))
}

export const removeClass = (el: Element, cls: string) => {
  if (!el || !cls.trim()) return
  el.classList.remove(...classNameToArray(cls))
}

// function remove<T>(arr: T[], item: T) {
//   if (arr.length) {
//     var index = arr.indexOf(item);
//     if (index > -1) {
//       return arr.splice(index, 1)
//     }
//   }
// }

export const remove = <T>(arr: T[], item: T) => {
  if (arr.length) {
    var index = arr.indexOf(item);
    if (index > -1) {
      return arr.splice(index, 1)
    }
  }
}

export const isString = (val: unknown): val is string =>
  typeof val === "string";